package dimes.controller;


import javax.annotation.Resource;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import dimes.dao.IUsersDao;
import dimes.model.Users;
import dimes.util.JWTUtil;
import dimes.util.ResponseData;


@RestController
@RequestMapping("/mall/html/users")
public class UsersController {

	@Resource
	private IUsersDao usersDao;
	
	@PostMapping("login.do")
	public ResponseData login(Users user) {
		Users u = usersDao.login(user);
		ResponseData responseData = ResponseData.ok();
		if(null!=u) {//数据库验证通过
			//生成token
    		String token = JWTUtil.generToken("1", "Jersey-Security-Basic", user.getPhone());
    		//向浏览器返回token，客户端受到此token后存入cookie中，或者h5的本地存储中
    		responseData.putDataValue("token", token);
    		//以及用户
    		responseData.putDataValue("user", u);
		}else {
			//用户或者密码错误
    		responseData=ResponseData.customerError();
		}
		return responseData;
	}
}
